// pages/share/share.js
var app = getApp();
var url = '';
Page({
  data: {
  },
  onLoad: function (options) {
    var nickname = wx.getStorageSync('nickname');
    if (!nickname) {
      wx.getUserInfo({
        withCredentials: false,
        success: function(res){
          wx.setStorageSync('nickname', res.userInfo.nickName);
        }
      });
    }

    var that = this;
    url = '/pages/products/products_flex';
    //来自小程序码扫码
    if (options.scene) {
      var productInfo = decodeURIComponent(options.scene)
      productInfo = productInfo.split('-');
      if (productInfo.length > 0) {
        var product_id = productInfo[0].split(':')[1];
        var shop_id = productInfo[1].split(':')[1];

        options = {
          product_share: true,
          shop_id: shop_id,
          product_id: product_id
        };

        wx.setStorageSync('current_shop_id', shop_id);
        wx.setStorageSync('current_shop_url', url);
      }
    }

    //来自商品详情页的分享
    if (options.product_share) {
      //如果是新用户
      var token = wx.getStorageSync('token');
      if (!token) {
        app.util.newLogin(app, function (res) {
          if (res) {
            var shop_id = options.shop_id;
            var product_id = options.product_id;
            wx.setStorageSync('current_shop_id', shop_id);
            wx.setStorageSync('current_shop_url', url);

            wx.redirectTo({
              url: '/pages/products/product?shop_id=' + shop_id + '&product_id=' + product_id + '&share=true'
            });
          }
        });
      } else {
        var shop_id = options.shop_id;
        var product_id = options.product_id;
        wx.setStorageSync('current_shop_id', shop_id);
        wx.setStorageSync('current_shop_url', url);

        wx.redirectTo({
          url: '/pages/products/product?shop_id=' + shop_id + '&product_id=' + product_id + '&share=true'
        });
      }
    }

    //来自店铺的分享
    if (options.products_share) {
      //如果是新用户
      var token = wx.getStorageSync('token');
      if (!token) {
        app.util.newLogin(app, function (res) {
          if (res) {
            app.func.req('shops/show', {
              id: options.shop_id
            }, 'GET', function (res) {
              that.setData({
                shop: res.result
              });

              if (res && res.result.layout == 2) {
                url = '/pages/products/products';
              }
              wx.setStorageSync('current_shop_id', options.shop_id);
              wx.setStorageSync('current_shop_url', url);

              wx.redirectTo({
                url: url + '?shop_id=' + options.shop_id + '&share=true'
              });
            }, {
                'token': wx.getStorageSync('token')
              });

          }
        });
      } else {
        app.func.req('shops/show', {
          id: options.shop_id
        }, 'GET', function (res) {
          that.setData({
            shop: res.result
          });

          if (res && res.result.layout == 2) {
            url = '/pages/products/products';
          }
          wx.setStorageSync('current_shop_id', options.shop_id);
          wx.setStorageSync('current_shop_url', url);

          wx.redirectTo({
            url: url + '?shop_id=' + options.shop_id + '&share=true'
          });
        }, {
            'token': wx.getStorageSync('token')
        });
      }
    }
  }
})
